package com.crm.sys.service.impl;

import java.io.File;
import java.util.ArrayList;
import java.util.Calendar;
import java.util.List;

import org.apache.commons.io.FileUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.web.multipart.MultipartFile;

import com.google.common.base.Strings;
import com.crm.sys.dao.FilesInfoDao;
import com.crm.sys.model.FilesInfo;
import com.crm.sys.service.FilesInfoService;
import com.crm.util.CommonUtil;
import com.crm.util.PropertyReader;
@Service("filesInfoService")
public class FilesInfoServiceImpl implements FilesInfoService {
	@Autowired
	private FilesInfoDao filesInfoDao;
	
	
	
	public int saveUserPicture(MultipartFile file, Long uid, String path) {
		int result = 0;
		if(Strings.isNullOrEmpty(file.getOriginalFilename())){
    	   return  result;
    	} 
    	String[]  name=	CommonUtil.splitFileName(file.getOriginalFilename());
    	boolean b =  CommonUtil.validateImgType(name[1]);
    	//文件类型验证通过 
    	if(b){
    	   try{
    		   File tempFile = new File(path, "B"+ uid+".jpg"); 
	           FileUtils.copyInputStreamToFile(file.getInputStream(),tempFile );  
	     	   result =1;
	     	   //大图裁剪
		     	 
	     	}catch(Exception e){
	     		e.printStackTrace();
	     	} 
    	} 
		return result;
	}
	
	public String savePicture(MultipartFile file, String newname, String path) {
		String fileUrl = "";
		if(Strings.isNullOrEmpty(file.getOriginalFilename())){
    	   return  fileUrl;
    	} 
    	String[]  name=	CommonUtil.splitFileName(file.getOriginalFilename());
    	boolean b =  CommonUtil.validateImgType(name[1]);
    	//文件类型验证通过 
    	if(b){
    	   try{
    		   File tempFile = new File(path,  newname+"."+name[1]); 
	           FileUtils.copyInputStreamToFile(file.getInputStream(),tempFile );  
	           fileUrl = newname+"."+name[1];
	     	   //大图裁剪
		     	 
	     	}catch(Exception e){
	     		e.printStackTrace();
	     	} 
    	} 
		return fileUrl;
	}
	
	public String saveFiles(MultipartFile file,String newname, String path) {
		String fileUrl = "";
		if(Strings.isNullOrEmpty(file.getOriginalFilename())){
    	   return  fileUrl;
    	} 
    	String[]  name=	CommonUtil.splitFileName(file.getOriginalFilename());
    	boolean b =  CommonUtil.validateFileType(name[1]);
    	//文件类型验证通过 
    	if(b){
    	   try{
    		   fileUrl = newname+"."+name[1];
    		   File tempFile = new File(path,  newname+"."+name[1]); 
	           FileUtils.copyInputStreamToFile(file.getInputStream(),tempFile );  
	     	}catch(Exception e){
	     		e.printStackTrace();
	     	} 
    	} 
		return fileUrl;
	}
	
	@Override
	public List<FilesInfo> saveFile(List<MultipartFile> list) {
		 List<FilesInfo>  fileList = new ArrayList<FilesInfo>();
	    for(MultipartFile file:list){
	    	if(Strings.isNullOrEmpty(file.getOriginalFilename())){
	    		continue;
	    	}
	    	FilesInfo fileInfo = new FilesInfo();
	    	fileInfo.setMime(file.getContentType());
	    	fileInfo.setNewmime(fileInfo.getMime());
	    	//fileInfo.setFilesize(file.getSize());
	    	String[]  name=	CommonUtil.splitFileName(file.getOriginalFilename());
	     	fileInfo.setName(name[0]);
	     	fileInfo.setSuffix(name[1]);
	    	
	    	String[] newName =CommonUtil.getNewFileName();
	    	fileInfo.setNewname(newName[0]);
	    	fileInfo.setNewsuffix(name[1]); //新文件后缀
	    	//fileInfo.setThumbnail(newName[2]);
	    	//fileInfo.setThumbnailsuffix(name[1]); //缩略图后缀
	    	
	    	boolean b = CommonUtil.validateFileType(name[1]);
	    	//文件类型验证通过 
	    	if(b){
	    		double size = file.getSize();
		     	size = Math.round(size/1024*100)*.01;
		     	fileInfo.setFilesize(size);
		     	
		    	Calendar calendar = Calendar.getInstance();
				/**
				 * 获取年份
				 */
				int year = calendar.get(Calendar.YEAR);
				/**
				 * 获取月份
				 */
				int month = calendar.get(Calendar.MONTH) + 1;
				String pathOne = year+""+month;
		     	String path = PropertyReader.loadPropertyParam("resource.properties", "fielPath")+"/"+pathOne+"/";
		     	try{
		     		File directory = new File(path);
		     		System.out.println("------"+directory.exists());
		     		 if(!directory.exists()) {
		     			 directory.mkdirs();//创建目录
		     			System.out.println("---ddddd---"+directory.exists());
		             }
		     	    FileUtils.copyInputStreamToFile(file.getInputStream(), new File(path, fileInfo.getNewname()+"."+fileInfo.getNewsuffix()));  
		     	   fileInfo.setPath(path);
		     
		     	}catch(Exception e){
		     		e.printStackTrace();
		     	}
		     	filesInfoDao.inserFilesInfo(fileInfo);
		  	   fileList.add(fileInfo);
	    	}
	    }
		return  fileList;
	}
	@Override
	public FilesInfo getFileInfoByID(FilesInfo finfo) {
		return filesInfoDao.getFilesInfoByID(finfo);
	}


}
